#include "PlatformProperty.h"

// 阅读灯开
#define READING_LIGHTS_SWITCH_ON 1
// 阅读灯关
#define READING_LIGHTS_SWITCH_OFF 2
// 消息 ID
#define MSG_RESPONSE_1 1
#define MSG_RESPONSE_2 2
#define MSG_RESPONSE_3 3
#define MSG_RESPONSE_4 4
#define MSG_RESPONSE_5 5

// 消息等待时间
#define WAITING_TIME 2 * 1000

// [SW_FC_008_002]Vehicle内灯_阅读灯开关Readinglights
SYSTEM_READ_WRITE_PROPERTY(READING_LIGHTS_SWITCH, int32_t) {
    // 总开关
    builder.area(VehicleAreaSeat::ROW_1_LEFT
                | VehicleAreaSeat::ROW_1_RIGHT
                | VehicleAreaSeat::ROW_2_LEFT
                | VehicleAreaSeat::ROW_2_RIGHT)
            .initValue([]() {
                return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &last) {
                VHALOG.d("READING_LIGHTS_SWITCH TOTAL cc526: %d, carMode: %d, usgMode: %d",
                        SIGNAL.getcarconfig526(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_03, sdb::CarConfig526::_05)) {
                    if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd) &&
                        SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModCrash,
                                            sdb::CarModSts1::CarModDyno)) {
                        return PropertyStatus::Active;
                    }
                    return PropertyStatus::NotActive;
                }
                return PropertyStatus::NotAvailable;
            }, SignalRX::carconfig526,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
            .withValueBySignals([this]([[maybe_unused]] int32_t last) {
                bool row1Le = SIGNAL.getReadLiStsFirstRowLe() == sdb::Boolean::TRUE;
                bool row1Ri = SIGNAL.getReadLiStsFirstRowRi() == sdb::Boolean::TRUE;
                bool row2Le = SIGNAL.getReadLiStsSecondRowLe() == sdb::Boolean::TRUE;
                bool row2Ri = SIGNAL.getReadLiStsSecondRowRi() == sdb::Boolean::TRUE;
                VHALOG.d("READING_LIGHTS_SWITCH TOTAL Valve Change last:[%d], row1Le[%d], row1Ri[%d], row2Le[%d], row2Ri[%d]",
                    last, row1Le, row1Ri, row2Le, row2Ri);
                removeMsg(MSG_RESPONSE_1);
                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_03)) {
                    if (row1Le && row1Ri) {
                        return toInt(COMMON_VALUE::COMMON_VALUE_ON);
                    } else {
                        return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
                    }
                } else if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_05)) {
                    if (row1Le && row1Ri && row2Le && row2Ri) {
                        return toInt(COMMON_VALUE::COMMON_VALUE_ON);
                    } else {
                        return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
                    }
                }
                return last;
            }, SignalRX::ReadLiStsFirstRowLe,
            SignalRX::ReadLiStsFirstRowRi,
            SignalRX::ReadLiStsSecondRowLe,
            SignalRX::ReadLiStsSecondRowRi)
            .onSetProperty([this](const int32_t &value) {
                if (value !=  toInt(COMMON_VALUE::COMMON_VALUE_ON) && value !=  toInt(COMMON_VALUE::COMMON_VALUE_OFF)) {
                    VHALOG.d("READING_LIGHTS_SWITCH TOTAL: param invaild");
                    return;
                }
                VHALOG.d("READING_LIGHTS_SWITCH TOTAL onSetProperty value[%d]", value);
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    SIGNAL.setCB_VFC_SetVehIntrLiCtrlForHmiCen(sdb::OnOff1::On);
                }
                ReadLiOpenReq groupValue;
                groupValue.readLiOpenReqFrontLeft = value? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                groupValue.readLiOpenReqFrontRight = value? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                groupValue.readLiOpenReqSecondRowLeft = value? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                groupValue.readLiOpenReqSecondRowRight = value? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                SIGNAL.setReadLiOpenReq(groupValue);
                sendMsg(MSG_RESPONSE_1, WAITING_TIME);
            })
            .onMsgEvent(MSG_RESPONSE_1, [this]() {
                VHALOG.d("READING_LIGHTS_SWITCH onMsgEvent MSG_RESPONSE_1");
                bool row1Le = SIGNAL.getReadLiStsFirstRowLe() == sdb::Boolean::TRUE;
                bool row1Ri = SIGNAL.getReadLiStsFirstRowRi() == sdb::Boolean::TRUE;
                bool row2Le = SIGNAL.getReadLiStsSecondRowLe() == sdb::Boolean::TRUE;
                bool row2Ri = SIGNAL.getReadLiStsSecondRowRi() == sdb::Boolean::TRUE;
                int32_t value = getValue(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT
                    | VehicleAreaSeat::ROW_1_RIGHT
                    | VehicleAreaSeat::ROW_2_LEFT
                    | VehicleAreaSeat::ROW_2_RIGHT));

                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_03)) {
                    if (row1Le && row1Ri) {
                        value = toInt(COMMON_VALUE::COMMON_VALUE_ON);
                    } else {
                        value = toInt(COMMON_VALUE::COMMON_VALUE_OFF);
                    }
                } else if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_05)) {
                    if (row1Le && row1Ri && row2Le && row2Ri) {
                         value = toInt(COMMON_VALUE::COMMON_VALUE_ON);
                    } else {
                        value = toInt(COMMON_VALUE::COMMON_VALUE_OFF);
                    }
                }
                setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT
                    | VehicleAreaSeat::ROW_1_RIGHT
                    | VehicleAreaSeat::ROW_2_LEFT
                    | VehicleAreaSeat::ROW_2_RIGHT),
                    value, false);
            })
            .area(VehicleAreaSeat::ROW_1_LEFT)
            .initValue([]() {
                return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &last) {
                VHALOG.d("READING_LIGHTS_SWITCH ROW_1_LEFT cc526: %d, carMode: %d, usgMode: %d",
                        SIGNAL.getcarconfig526(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_03, sdb::CarConfig526::_05)) {
                    if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd) &&
                        SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModCrash,
                                            sdb::CarModSts1::CarModDyno)) {
                        return PropertyStatus::Active;
                    }
                    return PropertyStatus::NotActive;
                }
                return PropertyStatus::NotAvailable;
            }, SignalRX::carconfig526,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
            .withValueBySignals([this]([[maybe_unused]] int32_t last) {
                removeMsg(MSG_RESPONSE_2);
                sdb::Boolean row1Le = SIGNAL.getReadLiStsFirstRowLe();
                VHALOG.d("READING_LIGHTS_SWITCH  ROW_1_LEFT Valve Change last:[%d], row1Le[%d]",
                    last, row1Le);
                return toInt(row1Le);
            }, SignalRX::ReadLiStsFirstRowLe)
            .onSetProperty([this](const int32_t &value) {
                if (value !=  toInt(COMMON_VALUE::COMMON_VALUE_ON) && value !=  toInt(COMMON_VALUE::COMMON_VALUE_OFF)) {
                    VHALOG.d("READING_LIGHTS_SWITCH ROW_1_LEFT: param invaild");
                    return;
                }
                VHALOG.d("READING_LIGHTS_SWITCH ROW_1_LEFT onSetProperty value[%d]", value);
                ReadLiOpenReq groupValue;
                groupValue.readLiOpenReqFrontLeft = value ? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                groupValue.readLiOpenReqFrontRight = 0;
                groupValue.readLiOpenReqSecondRowLeft = 0;
                groupValue.readLiOpenReqSecondRowRight = 0;
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    SIGNAL.setCB_VFC_SetVehIntrLiCtrlForHmiCen(sdb::OnOff1::On);
                }
                SIGNAL.setReadLiOpenReq(groupValue);
                sendMsg(MSG_RESPONSE_2, WAITING_TIME);
            })
            .onMsgEvent(MSG_RESPONSE_2, [this]() {
                VHALOG.d("READING_LIGHTS_SWITCH onMsgEvent MSG_RESPONSE_2");
                setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT), toInt(SIGNAL.getReadLiStsFirstRowLe()), false);
            })
            .area(VehicleAreaSeat::ROW_1_RIGHT)
            .initValue([]() {
                return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &last) {
                VHALOG.d("READING_LIGHTS_SWITCH ROW_1_RIGHT cc526: %d, carMode: %d, usgMode: %d",
                        SIGNAL.getcarconfig526(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_03, sdb::CarConfig526::_05)) {
                    if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd) &&
                        SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModCrash,
                                            sdb::CarModSts1::CarModDyno)) {
                        return PropertyStatus::Active;
                    }
                    return PropertyStatus::NotActive;
                }
                return PropertyStatus::NotAvailable;
            }, SignalRX::carconfig526,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
            .withValueBySignals([this]([[maybe_unused]] int32_t last) {
                removeMsg(MSG_RESPONSE_3);
                sdb::Boolean row1Ri = SIGNAL.getReadLiStsFirstRowRi();
                VHALOG.d("READING_LIGHTS_SWITCH  ROW_1_RIGHT Valve Change last:[%d], row1Ri[%d]",
                    last, row1Ri);
                return toInt(row1Ri);
            }, SignalRX::ReadLiStsFirstRowRi)
            .onSetProperty([this](const int32_t &value) {
                if (value !=  toInt(COMMON_VALUE::COMMON_VALUE_ON) && value !=  toInt(COMMON_VALUE::COMMON_VALUE_OFF)) {
                    VHALOG.d("READING_LIGHTS_SWITCH ROW_1_RIGHT: param invaild");
                    return;
                }
                VHALOG.d("READING_LIGHTS_SWITCH ROW_1_RIGHT onSetProperty value[%d]", value);
                ReadLiOpenReq groupValue;
                groupValue.readLiOpenReqFrontLeft = 0;
                groupValue.readLiOpenReqFrontRight = value ? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                groupValue.readLiOpenReqSecondRowLeft = 0;
                groupValue.readLiOpenReqSecondRowRight = 0;
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    SIGNAL.setCB_VFC_SetVehIntrLiCtrlForHmiCen(sdb::OnOff1::On);
                }
                SIGNAL.setReadLiOpenReq(groupValue);
                sendMsg(MSG_RESPONSE_3, WAITING_TIME);
            })
            .onMsgEvent(MSG_RESPONSE_3, [this]() {
                VHALOG.d("READING_LIGHTS_SWITCH onMsgEvent MSG_RESPONSE_3");
                setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT), toInt(SIGNAL.getReadLiStsFirstRowRi()), false);
            })
            .area(VehicleAreaSeat::ROW_2_LEFT)
            .initValue([]() {
                return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &last) {
                VHALOG.d("READING_LIGHTS_SWITCH ROW_2_LEFT cc526: %d, carMode: %d, usgMode: %d",
                        SIGNAL.getcarconfig526(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_05)) {
                    if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd) &&
                        SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModCrash,
                                            sdb::CarModSts1::CarModDyno)) {
                        return PropertyStatus::Active;
                    }
                    return PropertyStatus::NotActive;
                }
                return PropertyStatus::NotAvailable;
            }, SignalRX::carconfig526,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
            .withValueBySignals([this]([[maybe_unused]] int32_t last) {
                removeMsg(MSG_RESPONSE_4);
                sdb::Boolean row2Le = SIGNAL.getReadLiStsSecondRowLe();
                VHALOG.d("READING_LIGHTS_SWITCH  ROW_2_LEFT Valve Change last:[%d], row2Le[%d]",
                    last, row2Le);
                return toInt(row2Le);
            }, SignalRX::ReadLiStsSecondRowLe)
            .onSetProperty([this](const int32_t &value) {
                if (value !=  toInt(COMMON_VALUE::COMMON_VALUE_ON) && value !=  toInt(COMMON_VALUE::COMMON_VALUE_OFF)) {
                    VHALOG.d("READING_LIGHTS_SWITCH ROW_2_LEFT: param invaild");
                    return;
                }
                VHALOG.d("READING_LIGHTS_SWITCH ROW_2_LEFT onSetProperty value[%d]", value);
                ReadLiOpenReq groupValue;
                groupValue.readLiOpenReqFrontLeft = 0;
                groupValue.readLiOpenReqFrontRight = 0;
                groupValue.readLiOpenReqSecondRowLeft = value ? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                groupValue.readLiOpenReqSecondRowRight = 0;
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    SIGNAL.setCB_VFC_SetVehIntrLiCtrlForHmiCen(sdb::OnOff1::On);
                }
                SIGNAL.setReadLiOpenReq(groupValue);
                sendMsg(MSG_RESPONSE_4, WAITING_TIME);
            })
            .onMsgEvent(MSG_RESPONSE_4, [this]() {
                VHALOG.d("READING_LIGHTS_SWITCH onMsgEvent MSG_RESPONSE_4");
                setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_2_LEFT), toInt(SIGNAL.getReadLiStsSecondRowLe()), false);
            })
            .area(VehicleAreaSeat::ROW_2_RIGHT)
            .initValue([]() {
                return toInt(COMMON_VALUE::COMMON_VALUE_OFF);
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &last) {
                VHALOG.d("READING_LIGHTS_SWITCH ROW_2_RIGHT cc526: %d, carMode: %d, usgMode: %d",
                        SIGNAL.getcarconfig526(), SIGNAL.getVehModMngtGlbSafe1CarModSts1(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.carConfigIs(SignalRX::carconfig526, sdb::CarConfig526::_05)) {
                    if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd) &&
                        SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm, sdb::CarModSts1::CarModCrash,
                                            sdb::CarModSts1::CarModDyno)) {
                        return PropertyStatus::Active;
                    }
                    return PropertyStatus::NotActive;
                }
                return PropertyStatus::NotAvailable;
            }, SignalRX::carconfig526,
            SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1)
            .withValueBySignals([this]([[maybe_unused]] int32_t last) {
                removeMsg(MSG_RESPONSE_5);
                sdb::Boolean row2Ri = SIGNAL.getReadLiStsSecondRowRi();
                VHALOG.d("READING_LIGHTS_SWITCH  ROW_2_RIGHT Valve Change last:[%d], row2Ri[%d]",
                    last, row2Ri);
                return toInt(row2Ri);
            }, SignalRX::ReadLiStsSecondRowRi)
            .onSetProperty([this](const int32_t &value) {
                if (value !=  toInt(COMMON_VALUE::COMMON_VALUE_ON) && value !=  toInt(COMMON_VALUE::COMMON_VALUE_OFF)) {
                    VHALOG.d("READING_LIGHTS_SWITCH ROW_2_RIGHT: param invaild");
                    return;
                }
                VHALOG.d("READING_LIGHTS_SWITCH ROW_2_RIGHT onSetProperty value[%d]", value);
                ReadLiOpenReq groupValue;
                groupValue.readLiOpenReqFrontLeft = 0;
                groupValue.readLiOpenReqFrontRight = 0;
                groupValue.readLiOpenReqSecondRowLeft = 0;
                groupValue.readLiOpenReqSecondRowRight = value ? READING_LIGHTS_SWITCH_ON : READING_LIGHTS_SWITCH_OFF;
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    SIGNAL.setCB_VFC_SetVehIntrLiCtrlForHmiCen(sdb::OnOff1::On);
                }
                SIGNAL.setReadLiOpenReq(groupValue);
                sendMsg(MSG_RESPONSE_5, WAITING_TIME);
            })
            .onMsgEvent(MSG_RESPONSE_5, [this]() {
                VHALOG.d("READING_LIGHTS_SWITCH onMsgEvent MSG_RESPONSE_5");
                setValue(static_cast<int32_t>(VehicleAreaSeat::ROW_2_RIGHT), toInt(SIGNAL.getReadLiStsSecondRowRi()), false);
            });
}
